public class MinStack {

    LinkedList<Integer> stack;
    int min;

    /**
     * initialize your data structure here.
     */
    public MinStack() {
        stack = new LinkedList<>();
        min = Integer.MAX_VALUE;
    }

    public void push(int x) {
        stack.push(x);
        if(x < min) {
            min = x;
        }
    }

    public void pop() {
        int t = stack.pop();
        if(t == min) {
            updateMin();
        }
    }

    private void updateMin() {
        int m = Integer.MAX_VALUE;
        for(Integer i : stack) {
            if(i < m) {
                m = i;
            }
        }
        this.min = m;
    }

    public int top() {
        return stack.peek();
    }

    public int getMin() {
        return min;
    }
}